package com.ruanmou.vip.rabbitmq.demo.wq.fair;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.ruanmou.vip.rabbitmq.demo.util.RabbitMQUtil;

import java.util.concurrent.TimeUnit;

/**
 * 欢迎同学们来到牧码人教育
 *
 * @Classname Consumer
 * @Description TODO
 * @Date 2020-3-7 10:56
 * @Created by Gerry
 */
public class WQConsumer {
    public static void main(String[] args) throws Exception {
        // 创建Channel
        Channel channel = RabbitMQUtil.getConnection().createChannel();
        // 定义一个队列
        channel.queueDeclare("wq_queue1",false,false,false,null);
        // 设置Qos
        channel.basicQos(1);
        DeliverCallback deliverCallback = (consumerTag,message) -> {
            String messageString = new String(message.getBody(), "utf-8");
            System.out.println("接收到的信息:"+ messageString);
            try {
                TimeUnit.MILLISECONDS.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // 手动签收
                channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
            }
        };
        // 消费者消费到数据后自己签收，该条消息将会从对应队列中删除
        // true自动签收，false手动签收
        channel.basicConsume("wq_queue1",false,deliverCallback, consumerTag->{});
    }
}
